package com.ddn.controller.logistics;

import com.alibaba.excel.EasyExcel;
import com.ddn.mapper.logistics.dto.HqManagerExportyDto;
import com.ddn.model.logistics.HqManage;
import com.ddn.service.logistics.HqManagerService;

import com.ddn.vo.PageBean;
import com.ddn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/28 13:51
 */
@RestController
@RequestMapping("/api/hqmanager/")
public class HqManagerController {
    @Autowired
    private HqManagerService service;
    //新增
    @PostMapping("save.do")
    public R save(HqManage manage){
        return service.save(manage);
    }
    //更新
    @PostMapping("update.do")
    public R update(HqManage manage){
        return service.update(manage);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String name, int page, int limit){
        return service.pageByName(name,page,limit);
    }
    //导出数据为Excel表格
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1设置消息响应头
        response.setHeader("content-disposition","attachment;filename=manager"+System.currentTimeMillis()+".xlsx");
        //2.查询数据库中的数据
        List<HqManagerExportyDto> list=service.all();
        //3.实现数据导出Excel格式
        EasyExcel.write(response.getOutputStream(),
                HqManagerExportyDto.class).
                sheet(System.currentTimeMillis()+"").
                doWrite(list);
        response.getOutputStream().flush();
    }

    //导入数据
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
         return service.upload(file);
    }
    //海量导入数据
    @PostMapping("upload2.do")
    public R upload2(MultipartFile file) throws IOException {
         return service.upload2(file);
    }

    //删除
    @PostMapping("delbyids.do")
    public R delByIds(int[] ids){
        return service.delByIds(ids);
    }
    //查询物品数量
    @GetMapping("mnumtj.do")
    public R sexTj(){
        return service.numTj();
    }
    //查询种类数量
    @GetMapping("typetj.do")
    public R typeTj(){
        return service.typeTj();
    }
}
